﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace MediaData
{
    public class MediaRootManager
    {
        public DriveType[] TargetVolumeTypes { get; set; }
        public int MaxSearchDepth { get; set; }
        public string[] TypeDirectories { get; set; }
        private List<MediaRoot> roots = new List<MediaRoot>();
        private object rootsLock = new object();

        public MediaRootManager(params string[] typeDirectories)
        {
            this.TypeDirectories = typeDirectories;
            TargetVolumeTypes = new DriveType[] { DriveType.Fixed, DriveType.Network };
            MaxSearchDepth = 3;
        }

        public bool IsOnline(string volume)
        {
            
            return GetRoot(volume) != null;
        }

        public IEnumerable<MediaRoot> Roots
        {
            get {
                List<MediaRoot> r;
                lock (rootsLock)
                {
                    r = new List<MediaRoot>(roots);
                }
                return r;
            }
        }

        public string this[string volumeLabel] { get { return GetRoot(volumeLabel); } }

        public string GetRoot(string volumeLabel)
        {
            string retVal = null;
            lock (rootsLock)
            {
                foreach (var root in roots)
                {
                    if (root.Volume.Equals(volumeLabel, StringComparison.CurrentCultureIgnoreCase))
                    {
                        retVal = root.Path;
                        break;
                    }
                }
            }
            return retVal;
        }

        public void Refresh()
        {
            lock (rootsLock)
            {
                roots = new List<MediaRoot>();

                foreach (DriveInfo di in DriveInfo.GetDrives())
                    if (Array.IndexOf(TargetVolumeTypes, di.DriveType) > -1)
                    {
                        //if (di.DriveType == DriveType.Network && !Lizk.Utilities.Net.NetworkDrive.IsNetworkDriveOnline(di.RootDirectory.FullName, 1000))
                            //continue;

                        if (!di.IsReady)
                            continue;

                        string path = BFSRoot(new Queue<DirectoryNode>(new DirectoryNode[]{ new DirectoryNode(di.RootDirectory.FullName, MaxSearchDepth)}));
                        if (path == null)
                            continue;

                        roots.Add(new MediaRoot() { Volume = di.VolumeLabel, Path = path });
                    }

                roots.Sort();
            }
        }

        private string BFSRoot(Queue<DirectoryNode> queue)
        {
            while (queue.Count > 0)
            {
                DirectoryNode node = queue.Dequeue();

                try
                {
                    if (Path.GetPathRoot(node.Path) != node.Path)
                    {
                        FileAttributes attributes = File.GetAttributes(node.Path);
                        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                            continue;
                    }


                    
                    // Recursively call the subdirs
                    if (node.Depth > 0)
                    {
                        foreach (var subDir in Directory.GetDirectories(node.Path))
                        {
                            foreach (var type in TypeDirectories)
                            {
                                if (Path.GetFileName(subDir).Equals(type, StringComparison.CurrentCultureIgnoreCase))
                                    return node.Path;
                            }
                            queue.Enqueue(new DirectoryNode(subDir, node.Depth-1));
                        }
                    }
                }
                catch
                {
                    continue;
                }
            }
            return null;
        }

        private struct DirectoryNode { public string Path; public int Depth; public DirectoryNode(string path, int depth) { this.Path = path; this.Depth = depth; } }


    }


}
